package armserver.jms.wmq;

import javax.management.MBeanRegistration;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.apache.log4j.Logger;

import armserver.server.ArmServerConstant;
import armserver.server.ArmServerController;

public class JmsConnectFactory implements JmsConnectFactoryMBean,MBeanRegistration{
	
	private static final Logger log = Logger.getLogger(JmsConnectFactory.class);
	public static final String OBJECT_NAME="armServer.jms:name=JmsConnectFactory";
	public static final String INSTANCE_OBJ_NAME=ArmServerConstant.DEFAUTL_DOMAIN+".jms.instance:name=";
	private MBeanServer server;
	private ArmServerController controller = ArmServerController.getInstance();
	private String host;
	private int port;
	private int ccsid;
	private String channel;
	private String queueManager;
	private String queueName;
	private JmsConnection jmsConnection;
    private String connectionName;
    private static final int MQTYPE_WMQ=1;
    
	public void createWMQConnection() throws Exception {
		jmsConnection = new JmsConnection();
		jmsConnection.setConnectionName(connectionName);
		jmsConnection.setHost(host);
		jmsConnection.setPort(port);
		jmsConnection.setChannel(channel);
		jmsConnection.setCcsid(ccsid);
		jmsConnection.setQueueManager(queueManager);
		jmsConnection.setQueueName(queueName);
		controller.deployment(jmsConnection, INSTANCE_OBJ_NAME+connectionName);
	}

	public void createWMQConnection(String connectionName,String host, int port, String channel,
			int ccsid, String queueManagerName, String queueName)
			throws Exception {
		jmsConnection = new JmsConnection();
		jmsConnection.setConnectionName(connectionName);
		jmsConnection.setHost(host);
		jmsConnection.setPort(port);
		jmsConnection.setChannel(channel);
		jmsConnection.setCcsid(ccsid);
		jmsConnection.setQueueManager(queueManagerName);
		jmsConnection.setQueueName(queueName);
		controller.deployment(jmsConnection, INSTANCE_OBJ_NAME+connectionName);
	}

	public void setHost(String host) {
		this.host=host;
	}

	public void setPort(int port) {
		this.port=port;
	}

	public void setChannel(String channel) {
		this.channel=channel;
	}

	public void setCcsid(int ccsid) {
		this.ccsid=ccsid;
	}

	public void setQueueManager(String queueManagerName) {
		this.queueManager=queueManagerName;
	}

	public void setQueueName(String queueName) {
		this.queueName=queueName;
	}

	public void createConnetion() throws Exception {
		
		this.createWMQConnection();
	}

	public ObjectName preRegister(MBeanServer server, ObjectName name)
			throws Exception {
		this.server=server;
		return name!=null?name:ObjectName.getInstance(this.OBJECT_NAME);
	}

	public void postRegister(Boolean registrationDone) {
		
	}

	public void preDeregister() throws Exception {
		
	}

	public void postDeregister() {
		
	}

	public void setConnectionName(String connectionName) {
		this.connectionName=connectionName;
		
	}

	public String getHost() {
		return host;
	}

	public String getChannel() {
		return channel;
	}

	public int getPort() {
		return port;
	}

	public int getCcsid() {
		return ccsid;
	}

	public String getQueueManager() {
		return queueManager;
	}

	public String getQueueName() {
		return queueName;
	}

	public String getConnectionName() {
		return connectionName;
	}

}
